# try:
    
#     from vnpy.trader.object import PositionData
#     from pandas.core.frame import DataFrame
#     import re
#     import glob
#     from vnpy.app.cta_strategy import (
#         CtaTemplate,
#         StopOrder,
#         TickData,
#         BarData,
#         TradeData,
#         OrderData,
#         BarGenerator,
#         ArrayManager,
#     )
#     from .dw_client import *
#     from vnpy.trader.constant import (
#         Direction,
#         Offset,
#         Status
#     )
#     import pytz
#     from typing import List, Optional,Dict
#     import multiprocessing
#     from vnpy.trader.constant import (
#         Direction,
#         Offset,
#         OrderType,
#         Exchange
#     )
#     import threading
#     import time
#     import os

#     from vnpy_ctp.gateway.ctp_gateway import DIRECTION_VT2CTP, OFFSET_CTP2VT
#     from .time_factor import create_time_factor
#     from .factor import create_factor
#     import pandas as pd
#     import numpy as np
#     from queue import Queue
#     from datetime import date, datetime,time, timedelta, tzinfo
#     import pickle
#     from collections import deque
#     EXCHANGE_CTP2VT_ST: Dict[str, Exchange] = {
#         "CFFEX": Exchange.CFFEX,
#         "SHFE": Exchange.SHFE,
#         "CZCE": Exchange.CZCE,
#         "DCE": Exchange.DCE,
#         "INE": Exchange.INE
#     }

#     LIMIT_CTP2VT_ST:Dict[str,OrderType]={
#         "限价":OrderType.LIMIT,
#         "市价":OrderType.MARKET,
#         "STOP":OrderType.STOP,   
#         "FAK":OrderType.FAK,   
#         "FOK":OrderType.FOK,   
#         "询价":OrderType.RFQ
#     }    
#     DIRECTION_CTP2VT_ST:Dict[str,Direction]={
#         "多":Direction.LONG,
#         "空":Direction.SHORT,
#         "净":Direction.NET
#     }




#     OFFSET_CTP2VT_ST:Dict[str,Offset]={
#         "":Offset.NONE, 
#         "开":Offset.OPEN ,
#         "平":Offset.CLOSE,
#         "平今":Offset.CLOSETODAY,
#         "平昨":Offset.CLOSEYESTERDAY
#     }
#     STATUS_CTP2VT_ST={
#         "提交中":Status.SUBMITTING,
#         "未成交":Status.NOTTRADED,
#         "部分成交":Status.PARTTRADED,
#         "全部成交":Status.ALLTRADED,
#         "已撤销":Status.CANCELLED,
#         "拒单":Status.REJECTED
#     }
#     from peewee import CharField, DateField, IntegerField,  SqliteDatabase,Model,FloatField
#     db=SqliteDatabase('/root/.vntrader/order_database.db')

#     class OrderTable(Model):
#         gateway_name=CharField()
#         symbol=CharField()
#         exchange=CharField()
#         orderid=CharField()
#         ordersysid=CharField()
#         type=CharField()

#         direction=CharField()
#         offset=CharField()
#         price=CharField()

#         volume=FloatField()
#         traded=FloatField()
#         status=CharField()
#         datetime=DateField()
#         reference=CharField()
#         #直接存最后一个Order即可，把OrderID直接全部复刻过来，每次启动先去上面找东西，找不到就推一个初始化的数据，signal=0的时候有一组特定的输入
        
        
#         class Meta:
#             database=db
#             indexes=(
#                 (('symbol',),True),
#             )
#     @db.connection_context()
#     def update_database_orderdata(order:OrderData):
#         if len(OrderTable.select().where(OrderTable.symbol==order.symbol))==0:
#             # print(len(OrderTable.select().where(OrderTable.symbol=="pb")))
#             # print("unique")
#             if order.datetime==None:
#                 order.datetime=""
#             o=OrderTable(gateway_name=order.gateway_name,
#                         symbol=order.symbol,
#                         exchange=order.exchange.value,
#                         orderid=order.orderid,
#                         ordersysid=order.ordersysid,
#                         type=order.type.value,
#                         direction=order.direction.value,
#                         offset=order.offset.value,
#                         price=order.price,
#                         volume=order.volume,
#                         traded=order.traded,
#                         status=order.status.value,
#                         datetime=order.datetime,
#                         reference=order.reference)
#             o.save()
#         else:
#             if order.datetime==None:
#                 order.datetime=""
#             o=OrderTable.update({"symbol":order.symbol,
#                         "exchange":order.exchange.value,
#                         "orderid":order.orderid,
#                         "ordersysid":order.ordersysid,
#                         "type":order.type.value,
#                         "direction":order.direction.value,
#                         "offset":order.offset.value,
#                         "price":order.price,
#                         "volume":order.volume,
#                         "traded":order.traded,
#                         "status":order.status.value,
#                         "datetime":order.datetime,
#                         "reference":order.reference}
#             ).where(OrderTable.symbol==order.symbol)
#             o.execute()
#     @db.connection_context()
#     def get_database_orderdata(symbol)->Optional[OrderData]:
#         if len(OrderTable.select().where(OrderTable.symbol==symbol))==0:
#             return None
#         else:
#             o:OrderTable=list(OrderTable.select().where(OrderTable.symbol==symbol))[0]
#             if o.datetime=="":
#                 o.datetime=None
#             return OrderData(
#                 gateway_name=o.gateway_name, 
#                 symbol=o.symbol, 
#                 exchange=EXCHANGE_CTP2VT_ST[o.exchange], 
#                 orderid=o.orderid, 
#                 ordersysid=o.ordersysid, 
#                 type=LIMIT_CTP2VT_ST[o.type], 
#                 direction=DIRECTION_CTP2VT_ST[o.direction], 
#                 offset=OFFSET_CTP2VT_ST[o.offset], 
#                 price=o.price, 
#                 volume=o.volume, 
#                 traded=o.traded, 
#                 status=STATUS_CTP2VT_ST[o.status], 
#                 datetime=o.datetime, 
#                 reference=o.reference
#             )      
#     # update_database_orderdata(order)
#     # get_database_orderdata(order.symbol)
#     # p=Order()
#     # p._meta.table_name="order_12345"
#     # print(p._meta.table_name)
#     # p.create_table()
#     db.connect(reuse_if_open=True)
# except:
#     pass